<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="开始时间" prop="beginTime">
      <el-date-picker
      v-model="dataForm.beginTime"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-date-picker
      v-model="dataForm.endTime"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="实习阶段" prop="stage">
      <el-input v-model="dataForm.stage" placeholder="实习阶段"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="reportName">
      <el-input v-model="dataForm.reportName" placeholder="实习报告名称"></el-input>
    </el-form-item>
    <el-form-item label="实习报告" prop="file">
      <!-- <el-input type="file" v-model="dataForm.file" placeholder="实习报告名称"></el-input> -->
      <!-- <el-dialog
    title="上传文件"
    :close-on-click-modal="false"
    @close="closeHandle"
    :visible.sync="visible"> -->
    <el-upload
      drag
      :action="url"
      :before-upload="beforeUploadHandle"
      :on-success="successHandle"
      multiple
      :file-list="fileList"
      style="text-align: center;">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <!-- <div class="el-upload__tip" slot="tip">只支持jpg、png、gif格式的图片！</div> -->
    </el-upload>
  <!-- </el-dialog> -->
      <!-- <el-upload
            class="upload-import"
            ref="uploadImport"
            :http-request="httpRequest"
            action=""
            :on-remove="handleRemove"
            :file-list="fileList"
            :limit="1"
            :on-change="handleChange"
            :auto-upload="false">
          <el-button v-show="!hasFile" slot="trigger" size="small" type="primary" >选取文件</el-button>
        </el-upload> -->
      <!-- <el-upload
        class="upload-demo"
        action="https://jsonplaceholder.typicode.com/posts/"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="1"
        :on-exceed="handleExceed"
        :file-list="fileList">
      <el-button size="small" type="primary">点击上传</el-button>
      </el-upload> -->
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        url: '',
        num: 0,
        successNum: 0,
        fileList: [],
        dataForm: {
          id: 0,
          beginTime: '',
          endTime: '',
          stage: '',
          reportName: '',
          reportUrl: '',
          score: '',
          scoreDesc: '',
          stuId: '',
          createTime: '',
          file: ''
        },
        file: '',
        dataRule: {
          beginTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          stage: [
            { required: true, message: '实习阶段不能为空', trigger: 'blur' }
          ],
          reportName: [
            { required: true, message: '实习报告名称不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/internshipreport/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.beginTime = data.internshipReport.beginTime
                this.dataForm.endTime = data.internshipReport.endTime
                this.dataForm.stage = data.internshipReport.stage
                this.dataForm.reportName = data.internshipReport.reportName
                this.dataForm.reportUrl = data.internshipReport.reportUrl
                this.dataForm.score = data.internshipReport.score
                this.dataForm.scoreDesc = data.internshipReport.scoreDesc
                this.dataForm.stuId = data.internshipReport.stuId
                this.dataForm.createTime = data.internshipReport.createTime
              }
            })
          }
        })
        this.url = this.$http.adornUrl(`/manage/internshipreport/upload?token=${this.$cookie.get('token')}`)
        this.visible = true
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/internshipreport/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              // data: this.$refs['dataForm'].getFormData({ dataType: 'FormData' })
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'beginTime': this.dataForm.beginTime,
                'endTime': this.dataForm.endTime,
                'stage': this.dataForm.stage,
                'reportName': this.dataForm.reportName,
                'reportUrl': this.dataForm.reportUrl,
                'score': this.dataForm.score,
                'scoreDesc': this.dataForm.scoreDesc,
                'stuId': this.dataForm.stuId,
                'createTime': this.dataForm.createTime
              })
              // params: this.$http.adornParams(
              //   {
              //     'id': this.dataForm.id || undefined,
              //     'beginTime': this.dataForm.beginTime,
              //     'endTime': this.dataForm.endTime,
              //     'stage': this.dataForm.stage,
              //     'reportName': this.dataForm.reportName,
              //     'reportUrl': this.dataForm.reportUrl,
              //     'score': this.dataForm.score,
              //     'scoreDesc': this.dataForm.scoreDesc,
              //     'stuId': this.dataForm.stuId,
              //     'createTime': this.dataForm.createTime,
              //     'file': this.dataForm.file
              //   }
              // )
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // init (id) {
      //   this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
      //   this.visible = true
      // },
      // 上传之前
      beforeUploadHandle (file) {
        // if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
        //   this.$message.error('只支持jpg、png、gif格式的图片！')
        //   return false
        // }
        this.num++
      },
      // 上传成功
      successHandle (response, file, fileList) {
        this.fileList = fileList
        this.successNum++
        if (response && response.code === 0) {
          this.dataForm.reportUrl = response.url
          if (this.num === this.successNum) {
            this.$confirm('操作成功, 是否继续操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(() => {
              this.visible = false
            })
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      // 弹窗关闭时
      closeHandle () {
        this.fileList = []
        this.$emit('refreshDataList')
      }
    }
  }
</script>
